package ATM;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Properties;

public class pwdChangeFrame extends JFrame {
    Properties p = new Properties();
    private MyJText nowPwd = new MyJText("请输入旧密码",50,10,this);
    private MyJText newPwd = new MyJText("请输入新密码",50,90,this);
    private MyJText rePwd = new MyJText("确认新密码",50,130,this);

    public pwdChangeFrame(){

        this.setLayout(null);
        MyButton saveButton = new MyButton("改密码",350,200,this);
        this.setSize(500,300);
        this.setVisible(true);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
        this.setTitle("更换密码");

        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                pwdChange();
            }
        });
    }


    private void pwdChange(){
        try {
            p.load(new FileReader("Housework0906/src/ATM/ATM.txt"));
        }catch (Exception e){
            System.out.println("文件不存在");
        }
        if (!(p.getProperty("pwd").equals(nowPwd.getText()))){
            JOptionPane.showMessageDialog(null,"旧密码输入有误，重新输入");
        } else {
            if (newPwd.getText().equals(rePwd.getText())){
                JOptionPane.showMessageDialog(null,"修改成功");

                p.setProperty("pwd",newPwd.getText());
                try {
                    p.store(new FileWriter("Housework0906/src/ATM/ATM.txt"),null);
                }catch (Exception e){
                    System.out.println("文件不存在");
                }

                this.dispose();
                new ItemFrame();
            } else {
                JOptionPane.showMessageDialog(null,"确认密码有误");
            }

        }
    }

}
